﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Ivory.GUI
{
    public class MultiButton : GUIComponent
    {
        //Fields 
		private string _ButtonText; 
		public string ButtonText
		{
			get { return _ButtonText; }
            set { _ButtonText = value; this.ResizeComponent(); }
		} 
 
		private SpriteFont _ButtonFont; 
		public SpriteFont ButtonFont
		{
			get { return _ButtonFont; }
			set { _ButtonFont = value; }
		} 
 
		private Vector2 _ButtonPosition; 
		public Vector2 ButtonPosition
		{
			get { return _ButtonPosition; }
            set { _ButtonPosition = value; this.ResizeComponent(); }
		}
         
		private Color _ButtonColor = Color.White; 
		public Color ButtonColor
		{
			get { return _ButtonColor; }
			set { _ButtonColor = value; }
		}

        private Color _ButtonTextColor = Color.Yellow;
        public Color ButtonTextColor
        {
            get { return _ButtonTextColor; }
            set { _ButtonTextColor = value; }
        } 		
 
		private Color _ButtonHoverColor = Color.Yellow; 
		public Color ButtonHoverColor
		{
			get { return _ButtonHoverColor; }
			set { _ButtonHoverColor = value; }
		}

        private Texture2D _ButtonTexture;
        public Texture2D ButtonTexture
        {
            get { return _ButtonTexture; }
            set { _ButtonTexture = value; this.ResizeComponent(); }
        }

        protected string TextureLocation = string.Empty;
        protected Color ButtonRenderColor = Color.White;
								
        //Construct
        public MultiButton(Game game, SpriteFont font, string Text, string textureLocation, Vector2 Position)
            : base(game, new Rectangle((int)Position.X, (int)Position.Y, (int)font.MeasureString(Text).X, (int)font.MeasureString(Text).Y))
        {
            this.ButtonFont = font;
            this.ButtonText = Text;            
            this.TextureLocation = textureLocation;
            this.ButtonPosition = Position;

            this.OnMouseEnter += new EventHandler(OnMouseIn);
            this.OnMouseLeave += new EventHandler(OnMouseOut);
        }

        public MultiButton(Game game, SpriteFont font, string Text, Texture2D texture, Vector2 Position)
            : base(game, new Rectangle((int)Position.X, (int)Position.Y, (int)font.MeasureString(Text).X, (int)font.MeasureString(Text).Y))
        {
            this.ButtonFont = font;
            this.ButtonText = Text;           
            this.ButtonTexture = texture;
            this.ButtonPosition = Position; 

            this.OnMouseEnter += new EventHandler(OnMouseIn);
            this.OnMouseLeave += new EventHandler(OnMouseOut);
        }

        //Methods
        protected override void ResizeComponent()
        {
            ClickRectangle = new Rectangle(
                (int)ButtonPosition.X,
                (int)ButtonPosition.Y,
                (int)Math.Max(ButtonTexture == null ? 0 : ButtonTexture.Width, ButtonFont.MeasureString(ButtonText).X),
                (int)Math.Max(ButtonTexture == null ? 0 : ButtonTexture.Height, ButtonFont.MeasureString(ButtonText).Y));
        }

        public void OnMouseIn(object sender, EventArgs e)
        {
            ButtonRenderColor = ButtonHoverColor;
        }
        public void OnMouseOut(object sender, EventArgs e)
        {
            ButtonRenderColor = ButtonColor;
        }

        public override void DrawComponent(SpriteBatch batch, GameTime time)
        {
            batch.Draw(ButtonTexture, ClickRectangle, Color.White);
            batch.DrawString(ButtonFont, ButtonText, ButtonPosition, ButtonRenderColor);
        }
        public override void LoadComponent()
        {
            if (string.IsNullOrEmpty(TextureLocation))
            {
                if (ButtonTexture == null)
                    throw new IvoryFrameworkError(Errors.AssetLoadingException);
            }

            ButtonTexture = Game.Content.Load<Texture2D>(TextureLocation);
        }
    }
}
